<template>
	<view class="task-edit">
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-forms-item label="维护类型" name="maintenanceType">
				<uni-data-select v-model="formData.maintenanceType" :localdata="maintenanceTypes"
					placeholder="请选择维护类型" />
			</uni-forms-item>

			<uni-forms-item label="电梯" name="elevatorId">
				<uni-data-select v-model="formData.elevatorId" :localdata="elevatorOptions" placeholder="请选择电梯" />
			</uni-forms-item>

			<uni-forms-item label="维护人员" name="maintainerId">
				<uni-data-select v-model="formData.maintainerId" :localdata="maintainerOptions" placeholder="请选择维护人员" />
			</uni-forms-item>

			<uni-forms-item label="开始时间" name="startTime">
				<uni-datetime-picker v-model="formData.startTime" type="datetime" placeholder="请选择开始时间" />
			</uni-forms-item>

			<uni-forms-item label="预计时长(小时)" name="estimatedDuration">
				<uni-easyinput v-model="formData.estimatedDuration" type="number" placeholder="请输入预计时长" />
			</uni-forms-item>

			<uni-forms-item label="优先级" name="priority">
				<uni-data-select v-model="formData.priority" :localdata="priorityOptions" placeholder="请选择优先级" />
			</uni-forms-item>

			<uni-forms-item label="描述" name="description">
				<uni-easyinput v-model="formData.description" type="textarea" placeholder="请输入任务描述" />
			</uni-forms-item>
		</uni-forms>

		<button class="submit-btn" @click="handleSubmit">保存</button>
	</view>
</template>

<script>
	import {
		request
	} from '@/utils/request'

	export default {
		data() {
			return {
				taskId: '',
				formData: {
					maintenanceType: '',
					elevatorId: '',
					maintainerId: '',
					startTime: '',
					estimatedDuration: '',
					priority: '',
					description: ''
				},
				rules: {
					maintenanceType: {
						rules: [{
							required: true,
							errorMessage: '请选择维护类型'
						}]
					},
					elevatorId: {
						rules: [{
							required: true,
							errorMessage: '请选择电梯'
						}]
					},
					maintainerId: {
						rules: [{
							required: true,
							errorMessage: '请选择维护人员'
						}]
					},
					startTime: {
						rules: [{
							required: true,
							errorMessage: '请选择开始时间'
						}]
					},
					estimatedDuration: {
						rules: [{
							required: true,
							errorMessage: '请输入预计时长'
						}]
					},
					priority: {
						rules: [{
							required: true,
							errorMessage: '请选择优先级'
						}]
					}
				},
				maintenanceTypes: [{
						value: 'ROUTINE',
						text: '例行维护'
					},
					{
						value: 'REPAIR',
						text: '故障维修'
					},
					{
						value: 'INSPECTION',
						text: '安全检查'
					}
				],
				elevatorOptions: [],
				maintainerOptions: [],
				priorityOptions: [{
						value: 1,
						text: '低'
					},
					{
						value: 2,
						text: '中'
					},
					{
						value: 3,
						text: '高'
					}
				]
			}
		},

		onLoad(options) {
			this.taskId = options.id
			this.loadData()
		},

		methods: {
			async loadData() {
				try {
					const [task, elevators, maintainers] = await Promise.all([
						request({
							url: `/api/tasks/${this.taskId}`
						}),
						request({
							url: '/api/elevators'
						}),
						request({
							url: '/api/users',
							data: {
								role: 'MAINTAINER'
							}
						})
					])

					this.elevatorOptions = elevators.map(elevator => ({
						value: elevator.id,
						text: `${elevator.building} - ${elevator.location}`
					}))

					this.maintainerOptions = maintainers.map(user => ({
						value: user.id,
						text: user.username
					}))

					this.formData = {
						maintenanceType: task.maintenanceType,
						elevatorId: task.elevatorId,
						maintainerId: task.maintainerId,
						startTime: task.startTime,
						estimatedDuration: task.estimatedDuration,
						priority: task.priority,
						description: task.description
					}
				} catch (err) {
					uni.showToast({
						title: '加载数据失败',
						icon: 'none'
					})
				}
			},

			async handleSubmit() {
				try {
					await this.$refs.form.validate()

					await request({
						url: `/tasks/${this.taskId}`,
						method: 'PUT',
						data: this.formData
					})

					uni.showToast({
						title: '保存成功',
						icon: 'success'
					})

					uni.navigateBack()
				} catch (err) {
					if (err.errMsg) {
						uni.showToast({
							title: err.errMsg,
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: '保存失败',
							icon: 'none'
						})
					}
				}
			}
		}
	}
</script>

<style>
	.task-edit {
		padding: 15px;
	}

	.submit-btn {
		margin-top: 20px;
		background-color: #007AFF;
		color: #fff;
	}
</style>